package com.response; /**
 * @author wzm
 * @create 2023-04-28 10:39
 */

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/rs4")
public class ResponseServlet4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用IOUtils工具类完成流的复制

        //1.从磁盘上读取一个图片
        FileInputStream is = new FileInputStream("f:\\a.png");
        //2.获取response的字节输出流
        ServletOutputStream os = response.getOutputStream();
        //3.使用工具类完成流的复制（复制中就已经进行了字节流的输出）
        IOUtils.copy(is,os);
        //4.创建的流关闭
        is.close();

    }
}
